import matplotlib.pyplot as plt
from pathlib import Path
import csv
from datetime import datetime

from scipy.stats import alpha


def get_data(path,dates,highs,lows,index_date,index_high,index_low):
    """提取文件中的日期，最高与最低气温信息"""
    lines = Path(path).read_text().splitlines()
    reader = csv.reader(lines)
    header_row = next(reader)

    for row in reader:
        current_date = datetime.strptime(row[index_date],"%Y-%m-%d")
        try:
            high = int(row[index_high])
            low = int(row[index_low])
        except ValueError:
            print(f"Missing Data for {current_date}")
        else:
            dates.append(current_date)
            highs.append(high)
            lows.append(low)

# 获得锡特卡2021年的气温数据
path = Path("sitka_weather_2021_simple.csv")
dates, highs, lows = [], [], []
get_data(path,dates,highs,lows,index_date=2,index_high=4,index_low=5)

# 绘制锡特卡2021年气温图像
plt.style.use('seaborn-v0_8')
fig,ax = plt.subplots()
ax.plot(dates,highs,color='red',alpha=0.6)
ax.plot(dates,lows,color='blue',alpha=0.6)
ax.fill_between(dates,highs,lows,facecolor='blue',alpha=0.15)

# 获得死亡谷2021年的气温数据
path = Path("death_valley_2021_simple.csv")
dates,highs,lows = [],[],[]
get_data(path,dates,highs,lows,index_date=2,index_high=3,index_low=4)

# 绘制死亡谷2021年气温图像
ax.plot(dates,highs,color='red',alpha=0.3)
ax.plot(dates,lows,color='blue',alpha=0.3)
ax.fill_between(dates,highs,lows,facecolor='blue',alpha=0.05)

# 设置图像格式
title = "Daily high and low temperatures - 2021"
title += "\nSitka AK and Death_valley,CA"
ax.set_title(title,fontsize=14)
ax.set_xlabel('',fontsize=14)
fig.autofmt_xdate()
ax.set_ylabel("Temperature(F)",fontsize=14)
ax.tick_params(labelsize=16)
ax.set_ylim(10,140)

plt.show()
